package org.example.servicecounselor.controller;

import org.example.model.manage.bean.Result;
import org.example.model.manage.bean.po.Counselor;
import org.example.model.manage.bean.po.Major;
import org.example.model.manage.bean.po.Student;
import org.example.model.manage.bean.po.security.User;
import org.example.model.manage.bean.vo.CounselorRegisterRequest;
import org.example.model.manage.bean.vo.MajorCount;
import org.example.model.student.bean.Task;
import org.example.model.manage.bean.po.security.MyUserDetails;
import org.example.servicecounselor.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@PreAuthorize("hasAnyAuthority('counselor')")
public class CounselorController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private CounselorMajorService counselorMajorService;

    @Autowired
    private CounselorService counselorService;

    /**
     * 获取辅导员管理的专业列表
     */
    @GetMapping("/majorList")
    public Result<List<Major>> majorList() {
        User u = ((MyUserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser();
        List<Major> list = counselorMajorService.getMajorByCounselorId(u.getTypeId());
        return Result.success(list);
    }

    /**
     * 获取专业下的任务列表
     */
    @GetMapping("/getTask")
    public Result<List<Task>> getTask(Integer majorId) {
        List<Task> list = taskService.getByMajorId(majorId);
        return Result.success(list);
    }

    /**
     * 获取专业下的学生列表
     */
    @GetMapping("/getByMajor")
    public Result<?> countByMajor(Integer majorId) {
        return Result.success(studentService.getAllByMajorId(majorId));
    }

    /**
     * 获取专业学生统计信息
     */
    @GetMapping("/countByMajor")
    public Result<?> countByMajor() {
        return Result.success(studentService.countByMajor());
    }

    /**
     * 发送任务
     */
    @PostMapping("/sendTask")
    public Result<?> sendTask(@RequestBody Task task) {
        task.setUpdateTime(new Date());
        task.setCreateTime(new Date());
        taskService.sendTask(task);
        return Result.success(null);
    }

    /**
     * 修改任务
     */
    @PutMapping("/changeTask")
    public Result<?> changeTask(@RequestBody Task task) {
        int num = taskService.changeTask(task);
        return num == 1 ? Result.success(null) : Result.error(400, "修改失败");
    }

    /**
     * 删除任务
     */
    @DeleteMapping("/deleteTask")
    public Result<?> deleteTask(Integer majorId, Integer taskId) {
        int num = taskService.deleteTask(majorId, taskId);
        return num == 1 ? Result.success(null) : Result.error(400, "删除失败");
    }
}
